package org.szpshang.system.role.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.szpshang.bean.JsonBean;
import org.szpshang.bean.JsonDataBean;
import org.szpshang.system.role.entity.SysRole;
import org.szpshang.system.role.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public @ResponseBody JsonDataBean getRole(HttpServletRequest request) throws Exception {
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		return roleService.getRole(Integer.parseInt(start),Integer.parseInt(limit));
	}
	
	@RequestMapping(value = "/find", method = RequestMethod.POST)
	public @ResponseBody JsonDataBean find(HttpServletRequest request) throws Exception {
		String roleid = request.getParameter("roleid");
		return new JsonDataBean(true,roleService.getRole(Integer.parseInt(roleid)));
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody JsonBean add(HttpServletRequest request) throws Exception {
		SysRole sysRole = new SysRole();
		sysRole.setRoledesc(request.getParameter("roledesc"));
		roleService.addRole(sysRole);
		return new JsonBean(true);
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public @ResponseBody JsonBean edit(HttpServletRequest request) throws Exception {
		String roleid = request.getParameter("roleid");
		SysRole sysRole = roleService.getRole(Integer.parseInt(roleid));
		sysRole.setRoledesc(request.getParameter("roledesc"));
		roleService.editRole(sysRole);
		return new JsonBean(true);
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody JsonBean delete(HttpServletRequest request) throws Exception {
		String roleids = request.getParameter("roleids");
		roleService.deleteRole(roleids.split(","));
		return new JsonBean(true);
	}
	
}
